
// gcc look_at_small_deletions_select_probes.c -o look_at_small_deletions_select_probes.exe

//////cd /cygdrive/c/Users/voicu/work/ProjectsGr/OS/to_do_list/2013-05-21--15-16-07__SPECS__Look_at_small_deletions
//////cd /cygdrive/d/Projects/MyProjects/2013-07-02--18-34-17__SNP_Dels
//////gcc look_at_small_deletions_select_probes.c -o /cygdrive/d/Projects/MyProjects/2013-07-02--18-34-17__SNP_Dels/look_at_small_deletions_select_probes.exe
//////cd /cygdrive/c/Users/voicu/work/ProjectsGr/OS/perl


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <math.h>


///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//   Input parameters that need to be modified START
//
///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////

//#define INPUT_FILENAME  "BioM_purely_random.txt"
//#define OUPUT_FILENAME  "BioM_purely_random_NoOfDelsSelected.txt"

#define INPUT_FILENAME  "SNP_level_CN_250K.txt"
#define OUPUT_FILENAME  "Output__SNP_level_CN_250K.txt"

// number of lines in the input files including the header
#define NO_OF_LINES 262338

// number of columns in the input files including
#define NO_OF_COLUMNS 192

// number of biomarkers expected to be generated
#define PREDICTED_NO_OF_BIOMARKERS 150000

// number of lines for the buffer that will test the logR ration criterion
#define BUFFER_ROWS 5
////#define BUFFER_ROWS 2

// threshold for the numer of samples with logR ratio criterion met
// below this threshold biomarker is not reported
#define	GOT_COUNT_THRESHOLD 0


// in case the input file contains other columns than logR ratios
// if only logR ratio columns are present then SKIP_TO_EVERY_NTH_COLUMN equals 1
#define	SKIP_TO_EVERY_NTH_COLUMN    1


//////// criterion pattern
//////#define CRITERION_LogRRatio \
//////if ((bufferMat[0][jj]>1.2) && \
//////(bufferMat[1][jj]>1.2) && \
//////(bufferMat[2][jj]<(-1.2)) && \
//////(bufferMat[3][jj]>1.2) && \
//////(bufferMat[4][jj]>1.2))
//////// last line contains BUFFER_ROWS-1 in the first index of bufferMat
//////// additional criterions can be included



 //////////////criterion pattern
////////#define CRITERION_LogRRatio \
////////if ((bufferMat[0][jj]>1.0) && \
////////(bufferMat[1][jj]>1.0) && \
////////(bufferMat[2][jj]<(0.5)) && \
////////(bufferMat[3][jj]>1.0) && \
////////(bufferMat[4][jj]>1.0))
 ////////////////last line contains BUFFER_ROWS-1 in the first index of bufferMat
 ////////////////additional criterions can be included

//////////////// criterion pattern
//////////////#define CRITERION_LogRRatio \
//////////////df1 = fabs(bufferMat[0][jj]-bufferMat[1][jj]); \
//////////////df2 = fabs(bufferMat[1][jj]-bufferMat[2][jj]); \
//////////////df3 = fabs(bufferMat[2][jj]-bufferMat[3][jj]); \
//////////////df4 = fabs(bufferMat[3][jj]-bufferMat[4][jj]); \
//////////////\
//////////////if (  (df1 < 0.25) && \
      //////////////(df2 > 1) && \
      //////////////(df3 < 0.25) && \
      //////////////(df4 < 0.25) )

//////////////// last line contains BUFFER_ROWS-1 in the first index of bufferMat
//////////////// additional criterions can be included




//// criterion pattern
#define CRITERION_LogRRatio \
df1 = fabs(bufferMat[0][jj]-bufferMat[1][jj]); \
df2 = bufferMat[1][jj]-bufferMat[2][jj]; \
df3 = bufferMat[2][jj]-bufferMat[3][jj]; \
df4 = fabs(bufferMat[3][jj]-bufferMat[4][jj]); \
df5 = df2*df3;\
df2 = fabs(df2); \
df3 = fabs(df3); \
\
if (  (df1 < 0.1) && \
      (df2 > 0.5) && \
      (df3 > 0.5) && \
      (df4 < 0.1) && \
	  (df5 < 0) )

// last line contains BUFFER_ROWS-1 in the first index of bufferMat
// additional criterions can be included



///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//   Input parameters that need to be modified   END
//
///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////


#define BUFFER_COLS NO_OF_COLUMNS

#define fgetsMAXINP 100000

#define NON_NUMERIC_FIELD_LEN 1000


char buffer01[fgetsMAXINP], *pbuffer01;

// number of deletions per biomarker
int  NoOfDelsPerBioMarker[PREDICTED_NO_OF_BIOMARKERS], NoOfDelsPerBioMarkerCount=0;

// temporary count per biomarker
int GotCount;

// biomarker data set
int  StoreColData[PREDICTED_NO_OF_BIOMARKERS][BUFFER_COLS];
// temporary biomarker data set
int  ColData[BUFFER_COLS];

char  SPNProbeName[PREDICTED_NO_OF_BIOMARKERS][NON_NUMERIC_FIELD_LEN];
char  Chrom[PREDICTED_NO_OF_BIOMARKERS][NON_NUMERIC_FIELD_LEN];
char  Position[PREDICTED_NO_OF_BIOMARKERS][NON_NUMERIC_FIELD_LEN];

char  SPNProbeNameBBB[BUFFER_ROWS][NON_NUMERIC_FIELD_LEN];
char  ChromBBB[BUFFER_ROWS][NON_NUMERIC_FIELD_LEN];
char  PositionBBB[BUFFER_ROWS][NON_NUMERIC_FIELD_LEN];

char  SPNProbeNameTTT[NON_NUMERIC_FIELD_LEN];
char  ChromTTT[NON_NUMERIC_FIELD_LEN];
char  PositionTTT[NON_NUMERIC_FIELD_LEN];


// buffer matrrix that contains logR ratios for determining if chose criteria are met
double  bufferMat[BUFFER_ROWS][BUFFER_COLS];
double df1;
double df2;
double df3;
double df4;
double df5;

double tempF;

int offset, i, ii, jj, jj1;


FILE *fp, *fpout;

int main(int argc, char *argv[])
{

	fp = fopen(INPUT_FILENAME, "r");  // fastq input
	if(!fp)
	{
		printf("\nfp=0");
		return;
	}


	// get header line
	fgets(buffer01, fgetsMAXINP, fp);

	for(i=1;i<NO_OF_LINES;i++)
	{
		if ((i%1000)==0)
		{
			printf("\n%d", i);
		}

		fgets(buffer01, fgetsMAXINP, fp);
		////printf("\n%s", buffer01);

		pbuffer01 = &buffer01[0];

		//printf("\n\n%s",pbuffer01);

		sscanf(pbuffer01,"%s%n",&SPNProbeNameTTT,&offset);
		pbuffer01 += offset;
		//printf("\n\n%s",pbuffer01);
		sscanf(pbuffer01,"%s%n",&ChromTTT,&offset);
		pbuffer01 += offset;
		//printf("\n\n%s",pbuffer01);
		sscanf(pbuffer01,"%s%n",&PositionTTT,&offset);
		pbuffer01 += offset;
		//printf("\n\n%s",pbuffer01);


		// copy location info in the first line of the buffer
		strcpy(SPNProbeNameBBB[0],SPNProbeNameTTT);
		strcpy(ChromBBB[0],ChromTTT);
		strcpy(PositionBBB[0],PositionTTT);


		// shift buffer startting with the element before the last
		for(ii=(BUFFER_ROWS-1);ii>=1;ii--)
		{
			for(jj=0;jj<BUFFER_COLS;jj++)
			{
				bufferMat[ii][jj] = bufferMat[ii-1][jj];

			}

			strcpy(SPNProbeNameBBB[ii],SPNProbeNameBBB[ii-1]);
			strcpy(ChromBBB[ii],ChromBBB[ii-1]);
			strcpy(PositionBBB[ii],PositionBBB[ii-1]);
		}

		// read numerical data from the current line
		for(jj=0;jj<BUFFER_COLS;jj++)
		{
			sscanf(pbuffer01, " %lf%n", &bufferMat[0][jj], &offset);
			pbuffer01 += offset;
			////if (bufferMat[0][jj] > 1.2)
			////{
			////printf("\n%lf",bufferMat[0][jj]);
			////}

			// in case there's more data than just logR ratio and need to skip columns
			for(jj1=1; jj1<SKIP_TO_EVERY_NTH_COLUMN; jj1++)
			{
				sscanf(pbuffer01, " %lf%n", &tempF, &offset);
				pbuffer01 += offset;
			}
			//printf("\n\n%s",pbuffer01);

			////printf("\n%lf",bufferMat[0][jj]);
		}

		//exit(0);

		// if buffer is full
		if (i>=BUFFER_ROWS)
		{
			GotCount=0;
			for(jj=0;jj<BUFFER_COLS;jj++)
			{

				//printf("\n\n%s\n\n",buffer01);
				//exit(0);

				// check to see if criterion for logR ratio is met
				CRITERION_LogRRatio
				{
					GotCount++;
					ColData[jj]=1;
					////printf("\n%lf %lf %lf %lf %lf", bufferMat[0][jj], bufferMat[1][jj], bufferMat[2][jj],bufferMat[3][jj],bufferMat[4][jj]);
					////printf("\nLine: %d", i);
				}
				else
				{
					ColData[jj]=0;
				}



			}



			// check to see if the numer of samples with logR ratio criterion met is above threshold
			// if it is, store biomarker
			if (GotCount> GOT_COUNT_THRESHOLD)
			{
				NoOfDelsPerBioMarker[NoOfDelsPerBioMarkerCount] = GotCount;

				strcpy(SPNProbeName[NoOfDelsPerBioMarkerCount], SPNProbeNameBBB[2]);
				strcpy(Chrom[NoOfDelsPerBioMarkerCount], ChromBBB[2]);
				strcpy(Position[NoOfDelsPerBioMarkerCount], PositionBBB[2]);


				for(jj=0;jj<BUFFER_COLS;jj++)
				{
					StoreColData[NoOfDelsPerBioMarkerCount][jj] = ColData[jj];
				}

				NoOfDelsPerBioMarkerCount++;
				if (NoOfDelsPerBioMarkerCount % 1000 == 0)
				{
				printf("\n(POW)%d",NoOfDelsPerBioMarkerCount);
				}

			}

		}


	}

	fclose(fp);




	// store biomarker data to file
	fpout = fopen(OUPUT_FILENAME, "w");
	if(!fpout)
	{
		printf("\nfpout=0");
		return;
	}

	for(i=0;i<NoOfDelsPerBioMarkerCount;i++)
	{
		fprintf(fpout, "\n%s\t%s\t%s\t%d", SPNProbeName[i], Chrom[i],Position[i],NoOfDelsPerBioMarker[i]);
		for(jj=0;jj<BUFFER_COLS;jj++)
		{
			fprintf(fpout, "\t%d", StoreColData[i][jj]);
		}
	}

	fflush(fpout);
	fclose(fpout);
}